

var person = {
    name: 'zs',
    age: 1,
    children: {
        first: {
            name: "ls",
            age: 10
        },
        second: {
            name: "ww",
            age: 15
        }

    },
    car: ['Benz', 'Xiaomi']
}

// 浅拷贝
function clone(origin, target) {
    var tar = target || {}
    for (const key in origin) {
        if (Object.prototype.hasOwnProperty.call(origin, key)) {
            const element = origin[key];
            tar[key] = element
        }
    }
    return tar
}
// 深拷贝
function deepClone(origin, target) {
    var tar = target || {}
    var toStr = Object.prototype.toString
    var arrayType = '[object Array]'
    for (const key in origin) {
        if (Object.prototype.hasOwnProperty.call(origin, key)) {
            const element = origin[key]
            if (typeof origin[key] == 'object' && origin[key] !== null) {
                if (toStr.call(origin[key]) == arrayType) {
                    tar[key] = []
                } else {
                    tar[key] = {}
                }
                deepClone(origin[key], tar[key])
            } else {
                tar[key] = origin[key]
            }

        }
    }
    return tar
}


let p = deepClone(person)
p.children.third = {
    name: "zl",
    age: 18
}
console.log(p, person);
